package com.gitee.xmhzzz.component.things.metadata;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SimplePropertyMetadata
 * @Description TODO
 * @Author wzq
 * @Date 2024/5/29 14:21
 * @Version 1.0
 */
@Getter
@Setter
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class SimplePropertyMetadata implements PropertyMetadata {

    private DataType valueType;

    private String id;

    private String name;

    private String description;

    private Map<String, Object> expands;

    public static SimplePropertyMetadata of(String id, String name, DataType type) {
        SimplePropertyMetadata metadata = new SimplePropertyMetadata();
        metadata.setId(id);
        metadata.setName(name);
        metadata.setValueType(type);
        return metadata;
    }


    @Override
    public PropertyMetadata merge(PropertyMetadata another, MergeOption... option) {
        SimplePropertyMetadata metadata = BeanUtil.toBeanIgnoreError(this, SimplePropertyMetadata.class);
        if (metadata.expands == null) {
            metadata.expands = new HashMap<>();
        }
        if (CollUtil.isNotEmpty(another.getExpands())) {
            another.getExpands().forEach(metadata.expands::put);
        }
        return metadata;
    }
}
